//
//  PasswordViewController.m
//  SecretFolder
//
//  Created by Deepak Kumar on 24/06/12.
//  Copyright 2012 Frog. All rights reserved.
//

#import "PasswordViewController.h"
#import "PasswordchoiceViewController.h"

@implementation PasswordViewController
@synthesize mTableView;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.navigationItem.title = @"Set Password";
	self.navigationItem.rightBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Change"
									  style:UIBarButtonItemStyleDone
									 target:self
									 action:@selector(changePasswordClicked:)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(goback:)];
    
    self.navigationItem.hidesBackButton = YES;
    NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:17],UITextAttributeFont, nil];
    self.navigationController.navigationBar.titleTextAttributes = size;
    
    //[item release];
	
}

- (void) changePasswordClicked:(id)sender
{
	if (![oldPasswordTextField.text length] || ![newPasswordTextField.text length]) {
		// Please fill in your old and new passwords appropriately
		UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please fill in your password and confirm password appropriately." message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show]; // Please fill in all three fields.
       // [alert release];
		return;
	}
	else if([newPasswordTextField.text isEqualToString:oldPasswordTextField.text] == NO)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The two passwords you entered do not match." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
      //  [alert release];
		return;
    }

	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Your password has been set successfully." delegate:[PasswordchoiceViewController class] cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [[NSUserDefaults standardUserDefaults] setInteger:2 forKey:kPasswordType];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kPattern];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kPattern1];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:kPattern2];
	[alert show];
	//[alert release];
	[[NSUserDefaults standardUserDefaults] setObject:newPasswordTextField.text forKey:kPasswordText];
	[self.navigationController popToRootViewControllerAnimated:YES];
    [self.view reloadInputViews];
  //  [[[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-2] tableView] reloadData];
}


- (void) goback:(id)sender
{
    [oldPasswordTextField resignFirstResponder];
    [newPasswordTextField resignFirstResponder];
    [self disablesAutomaticKeyboardDismissal];
    NSArray *currentControllers = self.navigationController.viewControllers;
    
    NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
    
    NSLog(@"---------------count of navigatioin controllers %d %@",[newControllers count],newControllers);
    if (([newControllers count]-3)>0&&[newControllers objectAtIndex:([newControllers count]-2)]) {
        
        [newControllers removeObjectAtIndex:([newControllers count]-2)];
        [self.navigationController setViewControllers:newControllers animated:YES];
        
        [self.navigationController popViewControllerAnimated:YES];
        
        [[self.navigationController.viewControllers objectAtIndex:[self.navigationController.viewControllers count]-1] reloadData];
    }
    else
    {
         
    [newControllers removeLastObject];
        [self.navigationController setViewControllers:newControllers animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    }
            
    
}
- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 2;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		[cell.textLabel setFont:[UIFont systemFontOfSize:12.0]];
	}
	
	[cell.textLabel setTextColor:[UIColor colorWithRed:(97.0f/255.0f) green:(90.0f/255.0f) blue:(90.0f/255.0f) alpha:1]];
    
	// Configure the cell...
	if (indexPath.row == 0) {
		cell.textLabel.text = @"Password";
		
		if (oldPasswordTextField == nil) {
			oldPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 14, 150, 20)];
			oldPasswordTextField.delegate = self;
			oldPasswordTextField.secureTextEntry = YES;
			oldPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
			// Border Style None
			//[oldPasswordTextField setBorderStyle:UITextBorderStyleRoundedRect];
			oldPasswordTextField.font = [UIFont systemFontOfSize:12.0];
			oldPasswordTextField.placeholder = @"Enter password";
			oldPasswordTextField.returnKeyType = UIReturnKeyNext;
			oldPasswordTextField.tag = indexPath.row;
			oldPasswordTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
			
			[cell addSubview:oldPasswordTextField];
			
			[oldPasswordTextField becomeFirstResponder];
		}
	}
	else if (indexPath.row == 1) {
		cell.textLabel.text = @"Confirm Password";
		if (newPasswordTextField == nil) {
			newPasswordTextField = [[UITextField alloc] initWithFrame:CGRectMake(160, 14, 150, 20)];
			newPasswordTextField.delegate = self;
			newPasswordTextField.secureTextEntry = YES;
			newPasswordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
			// Border Style None
			//[newPasswordTextField setBorderStyle:UITextBorderStyleRoundedRect];
			newPasswordTextField.font = [UIFont systemFontOfSize:12.0];
			newPasswordTextField.placeholder = @"Enter confirm password";
			newPasswordTextField.returnKeyType = UIReturnKeyDone;
			newPasswordTextField.tag = indexPath.row;
			newPasswordTextField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
			
			[cell addSubview:newPasswordTextField];
		}
		
	}
    
	cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:15];
	return cell;
}
#pragma mark -
#pragma mark text field delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	[self.mTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:textField.tag inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *) textField 
{
	if (oldPasswordTextField == textField) {
		[newPasswordTextField becomeFirstResponder];
	}
	else if (newPasswordTextField == textField) {
		[newPasswordTextField resignFirstResponder];
	}
	
	
    return YES;
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Override to allow orientations other than the default portrait orientation.
    return UIInterfaceOrientationIsLandscape(interfaceOrientation)? YES : NO;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	self.mTableView = nil;
}


- (void)dealloc {
	
	if (oldPasswordTextField) {
		//[oldPasswordTextField release];
		oldPasswordTextField = nil;
	}
    
	if (newPasswordTextField) {
		//[newPasswordTextField release];
		newPasswordTextField = nil;
	}

	
   // [super dealloc];
}


@end
